package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class EditContactDialog extends Activity{
	/* private instance variables */
	CustomApp appState;
	String position_text;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.editcontactdialog);
        appState = ((CustomApp)getApplicationContext());
        Spinner spinner = (Spinner) findViewById(R.id.Edit_position_dropdown);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.positions_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new PositionSelectedListener());
        /* put current information in fields */
        Contact chosen_contact = getIntent().getParcelableExtra("contact");
   	 	TextView name = (TextView) findViewById(R.id.Edit_name_text);
   	 	name.setText(chosen_contact.get_name());
        EditText phone = (EditText) findViewById(R.id.Edit_phone_text);
        phone.setText(chosen_contact.get_phone());
        EditText address = (EditText) findViewById(R.id.Edit_address_text);
        address.setText(chosen_contact.get_address());
        EditText email = (EditText) findViewById(R.id.Edit_email_text);
        email.setText(chosen_contact.get_email());
    }
    
    public void okButtonHandler(View v){
    	/* gather the inputed information */
         EditText phone = (EditText) findViewById(R.id.Edit_phone_text);
         String phone_text = phone.getText().toString();
         EditText address = (EditText) findViewById(R.id.Edit_address_text);
         String address_text = address.getText().toString();
         EditText email = (EditText) findViewById(R.id.Edit_email_text);
         String email_text = email.getText().toString();
         /* build a new intent to return the data */
         Intent data = new Intent();
         data.putExtra("exists", false);
         data.putExtra("phone", phone_text);
         data.putExtra("address", address_text);
         data.putExtra("email", email_text);
         data.putExtra("position", position_text);
         setResult(Activity.RESULT_OK, data);
    	 finish();
    }
    
    /* class that monitors the position selector */
    public class PositionSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	position_text = parent.getItemAtPosition(pos).toString();

        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}